﻿
CREATE FUNCTION [dbo].[IsContInclus] (@SursaParam TipCont, @DestinatieParam TipCont, @TerminalSursa bit)  
RETURNS bit AS  
BEGIN 
	DECLARE @inclus bit
	DECLARE @terminal bit
	DECLARE @Sursa TipCont
	DECLARE @Destinatie TipCont
	
	SET @Sursa = LTRIM(RTRIM(@SursaParam))
	SET @Destinatie = LTRIM(RTRIM(@destinatieParam))
	
	SET @terminal = 1
	SET @inclus = 0
	-- daca sunt egale e ok
	IF (@Destinatie = @Sursa)
		RETURN 1
	IF (substring(@Destinatie,1,len(@Sursa))=@Sursa) AND (@TerminalSursa=0)
		SET @inclus = 1
		
	IF LEN(@Destinatie) > LEN(@Sursa)
	BEGIN
		DECLARE @pos2 int
		SET @pos2 = CHARINDEX('.',@Sursa) 
		IF  @pos2<>0
		BEGIN		
			DECLARE @ch  varchar(1)
			SET @ch = substring(@Destinatie,LEN(@Sursa)+1,1)  
			IF @ch<>'.'
				SET @terminal = 0
		END
		
	END
	IF (@inclus=1) AND (@terminal=1)
		RETURN  1
	RETURN 0
END